package com.ap.core.business.message.validation.handler.impl;

import com.ap.core.business.message.domain.translators.BindingMetaData;
import com.ap.core.business.message.validation.handler.AbstractValidationHandler;
import com.ap.core.business.message.validation.info.impl.RTPOriginalInfoForConfirmPaymentImpl;
import com.ap.core.common.types.InputMessageType;
import com.ap.core.common.types.MessageStatus;
import com.ap.core.common.types.ValidationType;
import com.ap.core.message.managepayment.info.ConfirmPaymentRequestMessageInfo;
import com.ap.core.validation.message.info.RTPOriginalInfoForConfirmPayment;
import com.ap.core.validation.message.info.MessageValidationInfo;

import java.util.EnumSet;
import java.util.Iterator;

/**
 * User: Rohit.Dixit
 * Implementation class for {@link com.ap.core.business.message.validation.handler.ValidationHandler} for SubmitRTPType message
 */
public class ConfirmPaymentValidationHandler extends AbstractValidationHandler<ConfirmPaymentRequestMessageInfo, RTPOriginalInfoForConfirmPayment> {

    private final static EnumSet<MessageStatus> processedInputMessages = EnumSet.of(MessageStatus.AUTHORISED, MessageStatus.DECLINED);


    @Override
    protected MessageValidationInfo<ConfirmPaymentRequestMessageInfo, RTPOriginalInfoForConfirmPayment> getMessageValidationInfo(final ConfirmPaymentRequestMessageInfo request, final BindingMetaData metaData) {
        return new MessageValidationInfo<ConfirmPaymentRequestMessageInfo, RTPOriginalInfoForConfirmPayment>() {
            @Override
            public ConfirmPaymentRequestMessageInfo getBoundObject() {
                return request;
            }

            @Override
            public RTPOriginalInfoForConfirmPayment getOriginalInfo() {
                return new RTPOriginalInfoForConfirmPaymentImpl(metaData.getOriginalReadOnlyOrderPayment(), metaData.getApMessageId());
            }
        };
    }

    @Override
    protected ValidationType getValidationType(BindingMetaData metaData) {
        final MessageStatus originalMessageStatus = metaData.getOriginalReadOnlyOrderPayment() == null ? null : metaData.getOriginalReadOnlyOrderPayment().getStatusCode();
        boolean isDuplicate = false;
        if(null != originalMessageStatus && processedInputMessages.contains(originalMessageStatus)){
            isDuplicate = true;
        }
        return ValidationType.valueOf(metaData.getInputMessageType(), isDuplicate);
    }
}
